package com.wrenchserver.controller.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wrenchserver.entity.sys.UserEntity;
import com.wrenchserver.pojo.sys.TUser;
import com.wrenchserver.service.sys.TUserService;
import com.wrenchserver.util.AjaxJson;

@Controller
@RequestMapping("/userController")  
public class UserController {
		@Autowired
	    private TUserService userService; 
		
		private static final Logger logger = Logger.getLogger(UserController.class);
		
		private String message;

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}
		/**
		 * 注册用户
		 * @param request
		 * @return
		 */
		@RequestMapping(params = "registerUser")
		public String chooseUser(HttpServletRequest request) {
			return "com/wrenchserver/sys/register";
		}
		
	    @RequestMapping(params = "showUser")
	    public String toIndex(HttpServletRequest request,Model model){  
	        int userId = Integer.parseInt(request.getParameter("id"));  
	        TUser user = this.userService.getUserById(userId);  
	        model.addAttribute("user", user);  
	        return "com/wrenchserver/sys/showUser";  
	        }
	    
	    @RequestMapping(params = "createUser")
	    @ResponseBody
	    public AjaxJson createUser(HttpServletRequest request,
				HttpServletResponse response){  
	    	String jsonStr = request.getParameter("jsonStr");
			AjaxJson ajaxJson = new AjaxJson();
			message = "用户注册成功";
			try {
				String returnJsonStr = userService.createUser(jsonStr);
				System.out.println(returnJsonStr);
				ajaxJson.setObj(returnJsonStr);
			} catch (Exception e) {
				e.printStackTrace();
				message = "用户注册失败：" + e.getMessage();
				logger.info(e.getMessage());
				ajaxJson.setSuccess(false);
			}
			ajaxJson.setMsg(message);
			return ajaxJson;
        }
	    //用户登录  
	    @RequestMapping(params = "login")
	    public String login(UserEntity user, HttpServletRequest request){  
	  
	        Subject subject=SecurityUtils.getSubject();  
	        UsernamePasswordToken token=new UsernamePasswordToken(user.getUserCde(),user.getPassWord());  
	        try {  
	            //调用subject.login(token)进行登录，会自动委托给securityManager,调用之前  
	            subject.login(token);//会跳到我们自定义的realm中  
	            request.getSession().setAttribute("user",user);  
	            return "success";  
	        }catch (Exception e){  
	            e.printStackTrace();  
	            request.getSession().setAttribute("user",user);  
	            request.setAttribute("error","用户名或密码错误");  
	            return "login";  
	        }  
	    }  
	  
	    @RequestMapping(params = "logout")
	    public String logout(HttpServletRequest request){  
	        request.getSession().invalidate();  
	        return "index";  
	    }  
	  
	    @RequestMapping(params = "admin")
	    public String admin(HttpServletRequest request){  
	        return "success";  
	    }  
	  
	    @RequestMapping(params = "student")
	    public String student(HttpServletRequest request){  
	        return "success";  
	    }  
	  
	    @RequestMapping(params = "teacher")
	    public String teacher(HttpServletRequest request){  
	        return "success";  
	    }  
}
